var getTableId = function (tagName) {
    var tableJSONs = JSON.parse(getSql("select table_id, tag_name from log_toc", {
        dbgroup: "mssql",
        offset: 0,
        limit: -1,
        skip: 0,
        outfmt: "json"
    }));
    var strRtn;
    for (var i = 0; i < tableJSONs.length; i++) {
        if (tableJSONs[i].tag_name == tagName) {
            strRtn = tableJSONs[i].table_id;
        }
    }
    return strRtn;
}
var getCurrentDiff = function (tagName) {
    var tableId = getTableId(tagName);
    debugString(tableId);
    var dailyData = JSON.parse(getSql("select max(nvalue) - min(nvalue) as 'daily_diff' from log_" + tableId + " where DATEDIFF(d, time_stamp, GETDATE()) = 0", {
        dbgroup: "mssql",
        offset: 0,
        limit: -1,
        skip: 0,
        outfmt: "json"
    }));
    return dailyData[0].daily_diff;
}
var getCurrentAvg = function (tagName) {
    var tableId = getTableId(tagName);
    var hourlyData = JSON.parse(getSql("select avg(nvalue) as 'hourly_avg' from log_" + tableId + " where DATEDIFF(d, time_stamp, GETDATE()) = 0", {
        dbgroup: "mssql",
        offset: 0,
        limit: -1,
        skip: 0,
        outfmt: "json"
    }));
    return hourlyData[0].hourly_avg;
}
// 回转链
setTag("BACKCHAIN_RPM_CURRENT", getCurrentAvg("FS04"));
setTag("BACKCHAIN_VIB_CURRENT", getCurrentAvg("FV04"));
setTag("BACKCHAIN_POW_CURRENT", getCurrentDiff("MB1_EP"));
// 主链
setTag("MAINCHAIN_RPM_CURRENT", getCurrentAvg("FS03"));
setTag("MAINCHAIN_VIB_CURRENT", getCurrentAvg("FV03"));
setTag("MAINCHAIN_POW_CURRENT", getCurrentDiff("MB6_EP"));
// 喷漆房
setTag("PAINT_PRESSURE_CURRENT", getCurrentAvg("PT01"));
setTag("PAINT_NEG_PRESSURE_CURRENT", getCurrentAvg("PT02"));
setTag("PAINT_ENV_TEMP_CURRENT", getCurrentAvg("RH01"));
setTag("PAINT_ENV_HUMID_CURRENT", getCurrentAvg("TT01"));
setTag("AUTO_PAINT_POW_CURRENT", getCurrentDiff("MB4_EP"));
setTag("MANUAL_PAINT_POW_CURRENT", getCurrentDiff("MB5_EP"));
setTag("PAINT_LITF_POW_CURRENT", getCurrentDiff("MB3_EP"));
// 烘房
setTag("DRYER_ENV_TEMP_CURRENT", getCurrentAvg("RH02"));
setTag("DRYER_ENV_HUMID_CURRENT", getCurrentAvg("TT02"));
setTag("DRYER_1_POW_CURRENT", getCurrentDiff("MB7_EP"));
setTag("DRYER_1_1TIC1_CURRENT", getCurrentAvg("TT03"));
setTag("DRYER_2_POW_CURRENT", getCurrentDiff("MB8_EP"));
setTag("DRYER_2_1TIC1_CURRENT", getCurrentAvg("TT04"));
setTag("DRYER_2_2T1C1_CURRENT", getCurrentAvg("TT05"));
setTag("DRYER_3_POW_CURRENT", getCurrentDiff("MB9_EP"));
setTag("DRYER_4_POW_CURRENT", getCurrentDiff("MB10_EP"));
// 强冷室
setTag("COOLER_BLOWER_POW_CURRENT", getCurrentDiff("MB11_EP"));
setTag("COOLER_DRAINER_POW_CURRENT", getCurrentDiff("MB12_EP"));
setTag("COOLER_1TIC1_CURRENT", getCurrentAvg("TT06"));
// 外围设备
setTag("FILTER_1_PRESS_CURRENT", getCurrentAvg("PT03"));
setTag("FILTER_2_PRESS_CURRENT", getCurrentAvg("PT04"));
setTag("FILTER_3_PRESS_CURRENT", getCurrentAvg("PT05"));
setTag("FILTER_4_PRESS_CURRENT", getCurrentAvg("PT06"));
setTag("FILTER_1_FLOW_CURRENT", getCurrentAvg("WS01"));
setTag("FILTER_2_FLOW_CURRENT", getCurrentAvg("WS02"));
setTag("FILTER_3_FLOW_CURRENT", getCurrentAvg("WS03"));
setTag("FILTER_4_FLOW_CURRENT", getCurrentAvg("WS04"));
setTag("PAINT_1_DRAINER_RPM_CURRENT", getCurrentAvg("FS01"));
setTag("PAINT_1_DRAINER_VIB_CURRENT", getCurrentAvg("FV01"));
setTag("PAINT_2_DRAINER_RPM_CURRENT", getCurrentAvg("FS02"));
setTag("PAINT_2_DRAINER_VIB_CURRENT", getCurrentAvg("FV02"));
setTag("WASTE_PUMP_POW_CURRENT", getCurrentDiff("MB2_EP"));
setTag("PUMP_1_POW_CURRENT", getCurrentDiff("MB13_EP"));
setTag("PUMP_2_POW_CURRENT", getCurrentDiff("MB14_EP"));
setTag("PAINT_1_DRAINER_POW_CURRENT", getCurrentDiff("MB17_EP"));
setTag("PAINT_1_BLOWER_POW_CURRENT", getCurrentDiff("MB15_EP"));
setTag("PAINT_2_DRAINER_POW_CURRENT", getCurrentDiff("MB18_EP"));
setTag("PAINT_2_BLOWER_POW_CURRENT", getCurrentDiff("MB16_EP"));